package com.tomato.order.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tomato.order.VO.ResultVO;
import com.tomato.order.config.ProjectUrlConfig;
import com.tomato.order.config.WechatConfig;
import com.tomato.order.entity.User;
import com.tomato.order.mapper.UserMapper;
import com.tomato.order.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@Controller
@Slf4j
public class LoginController {
    @Autowired
    private WechatConfig wechatConfig;
    @Autowired
    private ProjectUrlConfig urlConfig;

    @GetMapping("/login")
    public String login(@RequestParam("address") String address) {
        String api = "https://open.weixin.qq.com/connect/oauth2/authorize";
        String appid = wechatConfig.getAppId();
        String redirect_uri = URLEncoder.encode(String.format("http://%s/auth", urlConfig.getSell()));
        String url = String.format("%s?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=%s#wechat_redirect"
                , api, appid, redirect_uri, address);
        return "redirect:" + url;
    }

    @GetMapping("/auth")
    public String auth(@RequestParam("code") String code,
                       @RequestParam("state") String state) {
        String api = "https://api.weixin.qq.com/sns/oauth2/access_token";
        String appid = wechatConfig.getAppId();
        String appsecret = wechatConfig.getAppSecret();
        String url = String.format("%s?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
                api, appid, appsecret, code);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);
        Map map = JSON.parseObject(response);
        String redirect_uri = String.format("http://%s/getUserInfo?accessToken=%s&openid=%s&state=%s", urlConfig.getSell(),
                map.get("access_token"), map.get("openid"), state);
        return "redirect:" + redirect_uri;
    }

    @GetMapping("/getUserInfo")
    public String getUserInfo(@RequestParam("accessToken") String accessToken,
                              @RequestParam("openid") String openid,
                              @RequestParam("state") String state) {
        String api = "https://api.weixin.qq.com/sns/userinfo";
        String url = String.format("%s?access_token=%s&openid=%s", api, accessToken, openid);
        RestTemplate restTemplate = new RestTemplate();
        String response = null;
        response = new String(restTemplate.getForObject(url, String.class).getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        Map map = JSON.parseObject(response);
        String redirect_uri = String.format("http://%s/client/goods?country=%s&province=%s&city=%s&openid=%s&sex=%s&nickname=%s&headimgurl=%s&address=%s&/#/MyPage", urlConfig.getClient(),
                map.get("country"), map.get("province"), map.get("city"), map.get("openid"), map.get("sex"), URLEncoder.encode(map.get("nickname").toString()), map.get("headimgurl"), state);
        return "redirect:" + redirect_uri;
    }
}